﻿using AntdUI;
using WinFormium;

namespace TestPressure
{
    public partial class ProjectManageForm : BaseForm
    {

        #region 属性

        Project? obj;

        public Formium form;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Project> list = SqlSugarHelper.Inquireable<Project>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Project> list)
        {
            var antItemList = new List<ProjectItem>();
            foreach (Project item in list)
            {
                antItemList.Add(new ProjectItem(item.project_number, item.project_name, item.stake_mark, item.pipe_specification,
                                                 item.pipe_length, item.medium, item.design_pressure, item.pipe_nature, item.test_type,
                                                 item.company, item.manometer_precision, item.manometer_range, item.template_id));
            }
            Table.DataSource = antItemList;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(string number)
        {
            obj = ObjectWithPrimaryKey(number);
            if (obj == null)
            {
                LoadList();
                string msg = String.Format("编号【{0}】的工程不存在", number);
                Error(msg, this);
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>根据主键查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Project? ObjectWithPrimaryKey(string number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = number;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 系统方法
        public ProjectManageForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void ProjectManageForm_Load(object sender, EventArgs e)
        {
            Header.Text = "工程管理";

            Table.Height = Height - Header.Height - StackPanel1.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("project_number","工程编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("project_name","工程名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("stake_mark","桩号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("pipe_specification","管道规格",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("pipe_length","管道长度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("medium","试压介质",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("design_pressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("pipe_nature","试压性质",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("test_type","试压类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("company","施工单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("manometer_precision","压力表精度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("manometer_range","压力表量程",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("template_id","模板ID",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("operation","操作",AntdUI.ColumnAlign.Center)
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            }, () =>
            {

            });
        }
        #endregion

        #region 响应事件
        private void AddButton_Click(object sender, EventArgs e)
        {
            ProjectAddForm frm = new ProjectAddForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        /// <summary>点击表格操作按钮
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is ProjectItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    if (data.project_number is not null)
                    {
                        bool res = CheckRowExist(data.project_number);
                        if (!res)
                        {
                            return;
                        }
                    }
                    else
                    {
                        LoadList();
                        Error("工程信息错误", this);
                        return;
                    }

                    string msg = string.Format("您确定要删除【{0}】工程吗？", data.project_name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<Project> list = new List<Project>();
                                list.Add(obj);
                                int deleteRes = SqlSugarHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除工程失败", this);
                                }
                            }
                            else
                            {
                                Error("删除工程失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }
        private void Table_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (this.form is not null)
            {
                if (e.Record is not null)
                {
                    ProjectItem item = (ProjectItem)e.Record;
                    if (this.form is not null)
                    {
                        IndexFormium frm = (IndexFormium)this.form;
                        frm.projectItem = item;
                        frm.ReloadProjectInfo();
                    }
                    
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
        }

        #endregion
    }
}
